﻿//用于所有模块标识翻译添加、修改、删除、查询
var $dataAddTemplate = null;
var $dataListTemplate = null;
var $tbodyAdd = null;
var $tbodyList = null;
var ajaxUrl = "Handler.ashx";

//DOM樹加載完成
$(function () {
    $dataAddTemplate = $("#dataAddTemplate");
    $dataListTemplate = $("#dataListTemplate");
    $tbodyAdd = $("#tbodyAdd");
    $tbodyList = $("#tbodyList");

    getTranslateModuleTypeDDL();//获取翻译模块类型下拉框
    addEmptyRow(5);
    //getFlagTranslateList();
});

var $ddlModuleType = null;
var $ddlModule = null;
var moduleTypeDDLOptionsHtml = null;

//獲取翻譯模块类型下拉框
function getTranslateModuleTypeDDL() {
    $ddlModuleType = $("#ddlModuleType");
    $ddlModule = $("#ddlModule");

    var data = {
        "Operate": "GetTranslateModuleTypeDDL"
    };

    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            moduleTypeDDLOptionsHtml = $.templates($("#moduleTypeDDLTemplate").text()).render(ajaxMsg.data);
            $ddlModuleType.append(moduleTypeDDLOptionsHtml);

            var $ddlAddModuleType = $("#ddlAddModuleType");
            $ddlAddModuleType.append(moduleTypeDDLOptionsHtml);
            getFlagTranslateList();
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var moduleListData = null;

//獲取所有翻譯模块数据
function getAllTranslateModuleData()
{
    var data = {
        "Operate": "GetAllTranslateModuleDDL"
    };

    $.ajaxSetup({ async: false });
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            moduleListData = ajaxMsg.data;
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
    $.ajaxSetup({ async: true });
}

//根據模塊類型獲取模块
function getTranslateModuleDDL(currModuleTypeDDL, targetDDLModuleId) {
    var strModuleTypeId = $(currModuleTypeDDL).val();
    if (isNaN(strModuleTypeId)) {
        alert(txt_Error);
        return;
    }

    if (moduleListData == null)//没有模块数据
    {
        getAllTranslateModuleData();//获取所有模块数据
    }

    var moduleTypeId = parseInt(strModuleTypeId);
    var datas = [];
    for (var i = 0; i < moduleListData.length; i++) {
        if (moduleListData[i].TypeId == moduleTypeId)//模块类型Id与当前类型Id相等
        {
            datas.unshift(moduleListData[i]);
        }
    }

    if (moduleTypeId == 0)//如果是公共類型
    {
        datas.push({ "TypeId": 0, "ModuleID": 0, "Name": txt_CommonModule });//添加"公共模塊" 用於添加所有模塊共用的標識翻譯
    }

    var isEditModuleDDL = false;
    if (targetDDLModuleId.indexOf("_") != -1)//是列表中的模块下拉框
    {
        isEditModuleDDL = true;
        datas.unshift({ "TypeId": -1, "ModuleID": -1, "Name": txt_AllModules });//添加"所有模塊"
    }

    var htmlOptions = $.templates($("#moduleDDLTemplate").text()).render(datas);//填充模块下拉框
    var $targetDDL = $("#" + targetDDLModuleId);

    if (isEditModuleDDL==false){//不是列表中的模塊下拉框
        $targetDDL[0].options.length = 1;
    }
    else {//是列表中的模塊下拉框
        $targetDDL[0].options.length = 0;
    }

    $targetDDL.append(htmlOptions);
}

var flagIndex = 0;//標識索引

//添加空行
//rowCount 行數
function addEmptyRow(rowCount) {
    var datas = [];
    for (var i = 1; i <= rowCount; i++) {
        var data = {};
        data.ResultId = 0
        data.ModuleTypeId = 0;
        data.ModuleId = 0;
        data.FlagId = ++flagIndex;
        data.Flag = "";
        data.ResultHK = "";
        data.ResultCN = "";
        data.ResultEN = "";
        datas.push(data);
    }

    var html = $.templates($dataAddTemplate.text()).render(datas);
    $tbodyAdd.append(html);
}

//检查翻译标识是否存在
function isExistsTranslateFlag(txtFlag) {
    var flag = $(txtFlag).val();
    if (flag == "") {
        return;
    }

    var data = {
        "Operate": "IsExistsTranslateFlag",
        "Flag": flag
    };

    msgBox.showMsgWait(txt_IdentityCheck); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            msgBox.showMsgOk(txt_IdentityBeUsed); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
            txtFlag.focus();
        }
    }, "json");
}

//添加所有標識翻譯
function addAllFlagTranslate()
{
    var $ddlAddModuleType = $("#ddlAddModuleType");
    var strModuleTypeId = $ddlAddModuleType.val();
    if (isNaN(strModuleTypeId)) {
        alert(txt_ModuleTypError);
        return;
    }

    var moduleTypeId = parseInt(strModuleTypeId);
    if (moduleTypeId < 0) {
        alert(txt_SelectModuleType);
        $ddlAddModuleType.focus();
        return;
    }

    var $ddlAddModule = $("#ddlAddModule");
    var strModuleId = $ddlAddModule.val();
    if (isNaN(strModuleId)) {
        alert(txt_ModuleTypError);
        return;
    }

    var moduleId = parseInt(strModuleId);
    if (moduleId < 0) {
        alert(txt_SelectModuleType);
        $ddlAddModule.focus();
        return;
    }

    var $form = $("#formAdd");
    var data = $form.serialize();
    data += "&Operate=AddAllFlagTranslate&FlagCount=" + flagIndex + "&ModuleId=" + moduleId;

    msgBox.showMsgWait(txt_Save); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            var $tr = null;
            var strI = null;
            $tbodyAdd.children().each(function (i) {
                strI = ++i;
                $tr = $(this);
                $ddlAddModuleType.val("-1");
                $ddlAddModule.val("-1");
                $("#txtFlag_" + strI, $tr).val("");
                $("#txtResultHK_" + strI, $tr).val("");
                $("#txtResultCN_" + strI, $tr).val("");
                $("#txtResultEN_" + strI, $tr).val("");
            });

            getFlagTranslateList();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var isFirstLoad=true;//是否是首次加載數據
var sortField = null;
var isDesc = null;

//獲取排序字段和方法
function getSortInfo()
{
    sortField = "FlagId";
    isDesc = false;
    return;
}

//获取標識翻譯列表
function getFlagTranslateList() {
    var strModuleTypeId = $("#ddlModuleType").val();
    if (isNaN(strModuleTypeId)) {
        alert(txt_ModuleTypError);
        return;
    }

    var strModuleId = $("#ddlModule").val();
    if (isNaN(strModuleId)) {
        alert(txt_ModuleTypError);
        return;
    }

    var keyword=$("#txtKeyword").val();
    getSortInfo();//獲取排序信息

    var keyword = $("#txtKeyword").val();
    var data = {
        "Operate": "GetFlagTranslateList",
        "ModuleTypeId": strModuleTypeId,
        "ModuleId": strModuleId,
        "SortField": sortField,
        "IsDesc": isDesc,
        "Keyword": keyword
    };

    msgBox.showMsgWait(txt_Loading); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (ajaxMsg.data.length > 0){//有数据
                var html = $.templates($dataListTemplate.text()).render(ajaxMsg.data);
                $tbodyList.html(html);                      
                $tbodyList.children().each(function (i) {
                    var $tr = $(this);
                    var strFlagId = $tr.attr("id").replace("tr_", "");
                    var $ddlEditModuleType = $("#ddlEditModuleType_" + strFlagId, $tr);
                    $ddlEditModuleType.append(moduleTypeDDLOptionsHtml);
                    $ddlEditModuleType.prepend($ddlModuleType.children().eq(0).clone());//將"所有模塊類型"添加到最前面
                    $ddlEditModuleType.val($ddlEditModuleType.attr("moduletypeid"));

                    var $ddlEditModule = $("#ddlEditModule_" + strFlagId, $tr);
                    getTranslateModuleDDL($ddlEditModuleType[0], $ddlEditModule.attr("id"));
                    $ddlEditModule.val($ddlEditModule.attr("moduleid"));
                });
            }
            else {//无数据
                $tbodyList.children().remove();
                $tbodyList.html("<tr><td colspan=" + $tbodyList.attr("colspan") + ">" + txt_NoData + "</td></tr>");
            }

            msgBox.hidBox(); //隐藏消息面板
            return;//以下为分页代码，暂时没用

            var colspan=$tbody.prev().children().eq(0).children().length;
            if ($tbodyList.children().length > 0) {//如果已有数据，先移去
                $tbodyList.children().remove();
            }

            if(ajaxMsg.data.length>0)
            {
                $dataListTemplate.tmpl(ajaxMsg).appendTo($tbody); //添加数据

                $('#pagination-demo').twbsPagination({//分頁
                    totalPages: ajaxMsg.totalPages,
                    visiblePages: 10,
                    onPageClick: function (event, page) {
                        if(!isFirstLoad)
                        {                                    
                            getFiledList(page);
                        }
                        isFirstLoad=false;
                    },
                    first: txt_FirstPage,
                    prev: txt_PreviousPage,
                    next: txt_NextPage,
                    last: txt_LastPage
                });
            }
            else{
                $("<tr><td colspan='" + colspan + "'>" + txt_NoData + "</td></tr>").appendTo($tbody);
            }
            msgBox.hidBox(); //隐藏消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var flag = null;
var resultHK = null;
var resultCN = null;
var resultEN = null;

//修改標識翻譯
//btnSave 當前點擊的保存按鈕
function editFlagTranslate(btnEdit)
{
    var strFlagId = $(btnEdit).attr("id").replace("btnEdit_", "");
    if (isNaN(strFlagId)) {
        alert(txt_Error);
        return;
    }

    var flagId = parseInt(strFlagId);
    if (flagId <= 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + flagId, $tbodyList);

    var $ddlEditModuleType = $("#ddlEditModuleType_" + flagId, $tr);
    var strModuleTypeId = $ddlEditModuleType.val();
    if (isNaN(strModuleTypeId)) {
        alert(txt_ModuleTypError);
        return;
    }

    var moduleTypeId = parseInt(strModuleTypeId);
    if (moduleTypeId < 0) {
        alert(txt_SelectModuleType);
        $ddlEditModuleType.focus();
        return;
    }

    var $ddlEditModule = $("#ddlEditModule_" + flagId, $tr);
    var strModuleId = $ddlEditModule.val();
    if (isNaN(strModuleId)) {
        alert(txt_SelectModuleType);
        return;
    }

    var moduleId = parseInt(strModuleId);
    if (moduleId < 0) {
        alert(txt_SelectModuleType);
        $ddlEditModule.focus();
        return;
    }

    var $txtFlag = $("#txtFlag_" + flagId, $tr);
    var $txtResultHK = $("#txtResultHK_" + flagId, $tr);
    var $txtResultCN = $("#txtResultCN_" + flagId, $tr);
    var $txtResultEN = $("#txtResultEN_" + flagId, $tr);

    if ($txtFlag.length > 0 && $txtResultHK.length > 0 && $txtResultCN.length > 0 && $txtResultEN.length > 0)
    {
        if ($txtFlag.attr("id") != "txtFlag_" + strFlagId
            || $txtResultHK.attr("id").replace("txtResultHK_", "") != strFlagId
            || $txtResultCN.attr("id").replace("txtResultCN_", "") != strFlagId
            || $txtResultEN.attr("id").replace("txtResultEN_", "") != strFlagId)
        {
            alert(txt_Error);
            return;
        }

        flag = $txtFlag.val();
        resultHK = $txtResultHK.val();
        resultCN = $txtResultCN.val();
        resultEN = $txtResultEN.val();
        var data = {
            "Operate": "EditFlagTranslate",
            "ModuleId": moduleId,
            "FlagId": flagId,            
            "Flag": flag,
            "ResultHK": resultHK,
            "ResultCN": resultCN,
            "ResultEN": resultEN,
        };

        msgBox.showMsgWait(txt_TryingToModify); //显示消息面板
        $.post(ajaxUrl, data, function (ajaxMsg) {
            if (ajaxMsg.status == 1) {
                $ddlEditModuleType.attr("moduletypeid", moduleTypeId);//更新模塊類型Id
                $ddlEditModule.attr("moduleid", moduleId);//更新模塊Id
                msgBox.showMsgOk(txt_Success); //顯示消息面板
            }
            else {
                msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
            }
        }, "json");

        return;
    }
    else
    {
        alert(txt_Error);
        return;
    }
}

//刪除標識翻譯
//btnDelete 當前點擊的刪除按鈕
function deleteFlagTranslate(btnDelete)
{
    if (confirm(txt_SureDelete) == false) {
        return;
    }

    var strFlagId = $(btnDelete).attr("id").replace("btnDelete_", "");
    if (isNaN(strFlagId)) {
        alert(txt_Error);
        return;
    }

    var flagId = parseInt(strFlagId);
    if (flagId <= 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + flagId, $tbodyList);
    var data = {
        "Operate": "DeleteFlagTranslate",
        "FlagId": flagId
    };

    msgBox.showMsgWait(txt_TryingToDelete); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            $tr.remove();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");

    return;
}